package com.ruanyun.util;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 作者 hq.zheng:
 * @version 创建时间：2018-11-8 上午8:22:59
 * 时间工具类
 */
public class DormDateTimeUtil {
	 /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     * 
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author hq.zheng
     */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
 
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
 
        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);
 
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
 
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
	 /**
     * 获取当前月的第一天
     * 
     * @return
     * @author hq.zheng
     */
	public static Date getThisMonthFirstDay(){
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		return cale.getTime();
	}
	 /**
     * 获取当前月的最后一天
     * 
     * @return
     * @author hq.zheng
     */
	public static Date getThisMonthLastday(){
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return cale.getTime();
	}

/*
	public static void main(String[] args) throws ParseException {
		System.out.println(getNextMonday());
	}
*/

	/**
     * 获取下月的第一天
     * 
     * @return
     * @author hq.zheng
     */
	public static Date getPrecedMonthFirstDay(){
		Calendar cale = Calendar.getInstance();
		cale = Calendar.getInstance();
	    cale.add(Calendar.MONTH, 1);
	    cale.set(Calendar.DAY_OF_MONTH, 1);
	    return cale.getTime();
	}
	/**
     * 获取下月的最后一天
     * 
     * @return
     * @author hq.zheng
     */
	public static Date getPrecedMonthLastday(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar cale = Calendar.getInstance();
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 2);
	    cale.set(Calendar.DAY_OF_MONTH, 0);
	    return cale.getTime();
	}
	/**
     * 将字符串时间转成日期时间
     * 
     * @param nowDate 当前时间
     * @param format 转的格式，如[yyyy-MM-dd],[yyyy年MM月dd日 HH时mm分ss秒]
     * @return
     * @author hq.zheng
     */
	public static Date formatStringToDate(String nowDate,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date=null;
		try {
			date=sdf.parse(nowDate);
		} catch (Exception e) {
			e.printStackTrace();
			return date;
		}
		return date;
	}

	public static Date getFirstDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(date);
			cal.set(Calendar.DAY_OF_WEEK, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return cal.getTime();
	}

	public static Date getLastDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();

		try {
			cal.setTime(date);
			cal.set(Calendar.DAY_OF_WEEK, 1);
			cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 6);
		} catch(Exception e) {
			e.printStackTrace();
		}

		return cal.getTime();
	}


	// 获得当前日期与本周日相差的天数
	private static int getMondayPlus(Date gmtCreate) {
		Calendar cd = Calendar.getInstance();
		cd.setTime(gmtCreate);
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {
			return 0;
		} else {
			return 1 - dayOfWeek;
		}
	}


	// 获得下周星期一的日期
	public static String getNextMonday(Date parse){
		int mondayPlus = getMondayPlus(parse);
		Calendar cd = Calendar.getInstance();
		cd.setTime(parse);
		cd.add(Calendar.DATE, mondayPlus);
		Date monday = cd.getTime();
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		return simpleDateFormat.format(cd.getTime());
	}


	// 统计时间段内时长
	public static BigDecimal getHourCount(String date){
		String[] stringArray=date.split("-");
		String beginTime=stringArray[0];
		String endTime=stringArray[1];
		Integer beginTime1=getMin(beginTime);
		Integer endTime1=getMin(endTime);
		return (new BigDecimal(endTime1-beginTime1).divide(new BigDecimal(60), 2, BigDecimal.ROUND_HALF_UP));
	}

	public static Integer getMin(String time){
		String[] strings=time.split(":");
		return (Integer.valueOf(strings[0])*60+Integer.valueOf(strings[1]));
	}

}